/*
*	LimboEngine
*
*	Author:	Thomas Lim
*	Copyright(c) 2012
*
*	config.cpp
*/
#include "config.h"

Config::Config(void)
{
	attributes = new int[256];
	data = load();
}

Config::~Config(void)
{
	delete attributes;
}

/*
* DESCRIPTION:	Loads the config file, which has to be located in the app's root
*		directory. It also has to be named "config".
*
* PARAMETERS:	-
*
* RETURN VALUE:	returns a string with all settings in ascii
*/
char* Config::load(void)
{
	std::fstream in;
	in.open("config", std::ios::in);
	if(!in.is_open())
	{
		std::cerr << "[config.cpp] load(void) failed: file could not be opened.\n";
		return NULL;
	}
	else
	{
		in.seekg(0, std::ios::end);
		const int file_size = in.tellg();
		in.seekg(0, std::ios::beg);
		char *result = new char[file_size];
		in.read(result, file_size);
		in.close();
		return result;
	}
}

/*
* DESCRIPTION:	Saves the current setting of this config object into the
*		config file.
*
* PARAMETERS:	-
*
* RETURN VALUE:	true on success, false on error.
*/
bool Config::save(void)
{
	std::fstream out;
	out.open("config", std::ios::out);
	if(!out.is_open())
	{
		std::cerr << "[config.cpp] save(void) failed: file could not be opened.\n";
		return false;
	}
	else
	{
		out << data;
		out.close();
		return true;
	}
}

/*
* DESCRIPTION:	Gets each line out of a string and looks for matching attributes.
*		Attributes will be stored in attributes array. The indices are
*		defined in config.h. They are a enumeration. Look into that
*		header file to find out more.
*
* PARAMETERS:	char* attribs - the string to be worked with
*
* RETURN VALUE:	true on succes, false on error
*/
bool Config::apply(char* attribs)
{
	std::stringstream ss;
	std::string data(attribs);
	std::string line;
	std::string current_attribute;
	int line_start = 0;
	int line_stop = 0;
	int eq_index = 0;
	while((line_stop = data.find_first_of('\n', line_start)) != std::string::npos)
	{
		line = data.substr(line_start, line_stop);
		eq_index = line.find_first_of('=', 0);
		current_attribute = line.substr(0, eq_index);
		if(current_attribute.compare("VIDEO_RESOLUTION_WIDTH") == 0)
		{
			ss << line.substr(eq_index + 1, line.size());
			ss >> attributes[VIDEO_RESOLUTION_WIDTH];
		}
		if(current_attribute.compare("VIDEO_RESOLUTION_HEIGHT") == 0)
		{
			ss << line.substr(eq_index + 1, line.size());
			ss >> attributes[VIDEO_RESOLUTION_HEIGHT];
		}
		if(current_attribute.compare("VIDEO_RENDERING_MODE") == 0)
		{
			ss << line.substr(eq_index + 1, line.size());
			ss >> attributes[VIDEO_RENDERING_MODE];
		}
		if(current_attribute.compare("VIDEO_RESOLUTION_WIDTH") == 0)
		{
			ss << line.substr(eq_index + 1, line.size());
			ss >> attributes[VIDEO_FULLSCREEN];
		}
		line.clear();
		current_attribute.clear();
		line_start = line_stop + 1;
	}
	return true;
}

/*
* DESCRIPTION:	Prints the content stored in data onto the standard output
*		stream.
*
* PARAMETERS:	-
*
* RETURN VALUE: -
*/
void Config::print(void)
{
	std::cout << "===== config =====\n" << data << "=================\n";
}
